/***********************************************************************
    filename:   DaterCalendarYear.cpp
    created:    21/4/2012
    author:     4Ys

    purpose:    Implement the year class used in calendar
*************************************************************************/

#include "DaterCalendarYear.h"

namespace Dater
{
	//----------------------------------------------------------------------------//
	CalendarYear::CalendarYear(int year) :
	_year(year), _dayNum(365)
	{
		//judge whether this year is a leap year
		if( ( _year % 4 == 0 && _year % 100 != 0 ) || _year % 400 ==0 )
		{
			_dayNum++;
		}

		//_months[0] contains nothing
		_months.push_back(NULL);

		CalendarMonth* commonMP;
		for(unsigned int index1 = 1; index1 <= 12; index1++)
		{
			commonMP = new CalendarMonth(_year, index1);
			_months.push_back(commonMP);
		}
	}

	//----------------------------------------------------------------------------//
	CalendarYear::~CalendarYear()
	{
		CalendarMonth* commonMP;
		for(unsigned int index1 = 1; index1 <= 12; index1++)
		{
			commonMP = _months[index1];
			delete commonMP;
		}
	}

	//----------------------------------------------------------------------------//
	int CalendarYear::getYear()
	{
		return _year;
	}

	//----------------------------------------------------------------------------//
	CalendarMonth* CalendarYear::getMonthCalendar(int month)
	{
		if(month <= 12 && month >= 1)
			return _months[month];

		return NULL;
	}

	//----------------------------------------------------------------------------//
	void CalendarYear::addEvent(Event* _event)
	{
		tm* s = _event->getStartTime().getTime();
		tm* e = _event->getFinishTime().getTime();

		int s_m = (s->tm_year + 1900 == _year) ? s->tm_mon + 1 : 1;
		int e_m = (e->tm_year + 1900 == _year) ? e->tm_mon + 1 : 12;

		while(s_m <= e_m)
		{
			_months[s_m]->addEvent(_event);
			s_m++;
		}
	}

	void CalendarYear::deleteEvent(Event* _event)
	{
		tm* s = _event->getStartTime().getTime();
		tm* e = _event->getFinishTime().getTime();

		int s_m = (s->tm_year + 1900 == _year) ? s->tm_mon + 1 : 1;
		int e_m = (e->tm_year + 1900 == _year) ? e->tm_mon + 1 : 12;

		while(s_m <= e_m)
		{
			_months[s_m]->deleteEvent(_event);
			s_m++;
		}
	}
}